#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"

unsigned char KeyNum;
unsigned int Password,Count;
int main()
{
	LCD_Init();
	LCD_ShowString(1,1,"Password");
	while(1)
	{
		KeyNum = MatrixKey();
		if(KeyNum)
		{
			if(KeyNum<=10)//如果S1-S10按键按下，输入密码
			{
				if(Count<4)
				{
					Password = Password*10;//相当于密码左移一位
					Password  = KeyNum%10+Password;//获取一位密码
					Count++;
				}
				LCD_ShowNum(2,1,Password,4);
			}
			if(KeyNum==11)//确认键
			{
				if(Password==2345)
				{
					LCD_ShowString(1,14,"OK ");//密码正确
					Password=0;
					Count=0;
				}
				else 
				{
					LCD_ShowString(1,14,"err");//密码错误
					Password=0;//密码清零
					Count=0;//计次清零
					LCD_ShowNum(2,1,0,4);
				}
			}
			if(KeyNum==12)//
			{
				Password=0;//密码清零
				Count=0;//计次清零
				LCD_ShowNum(
				2,1,0,4);
			}
			
		}
	}
}